<?php


/**
 * 分页  计算展示的页码数
 * table 数据库
 * sql 查询语句
 * page 当前页码
 * num 当页展示个数
 * ge 页码显示个数
 */

function getPage($table,$sql,$page,$num,$ge){

    $count = M($table)->where($sql)->count();
    $all = ceil( $count / $num);
    if( $page <= 0) $page =0;
    if($page >= $all && $all != 0) $page = $all-1;
    $k = $ge;
    $p = floor( $page / $k );
    $res['total'] = $all;
    $res['count'] = $count;
    $res['page'] = $page;
    if($all == 0){
        $res['num']=0;
    }elseif( $p < 1 ){  //少于10页
        $p = $all <= $ge ? $all: $ge;
        for($i = 1; $i<= $p ; $i++)
            $res['num'][]=$i;
    }elseif( $p >= 1 && ($all - $page) >$ge ){ //中间页数
        for($i = $p*$k; $i <= $p*$k+$ge; $i++)
            $res['num'][]=$i;
    }else{ //最后10页内
        for($i = $all-$ge; $i <= $all; $i++)
            $res['num'][]=$i;
    }

    return $res;

}

    /**
     *  登录状态验证
     */
    function check_login(){
        $user_id = session('user_id');
        if(empty($user_id)){
            $user_id = cookie('user_id');
            if(empty($user_id) ){
                return 0;
            }else{
                $exit   =   M('users')->where(array('id'=>$user_id))->find();
                if(!$exit) return 0 ;
                session('user_id',cookie('user_id'));
                M("users")->where(array('id'=>$user_id))->save(array("login_time"=>getTime()));
                return 1;
            }
        }else{
            $exit   =   M('users')->where(array('id'=>$user_id))->find();
            if(!$exit) return 0 ;
            M("users")->where(array('id'=>$user_id))->save(array("login_time"=>getTime()));
            return 1;
        }
    }

    /**
     *  空数据判断
     */
    function emptyData($data){
        foreach($data as $key => $value)
            if(empty($value)) return false;
        return true;
    }

/**
 * 手机号判断
 */
function isMobile($phone){
    if(!preg_match("/^0?1[3|4|5|7|8][0-9]\d{8}$/",$phone)){
        return 0;
    }else{
        return 1;
    }
}

/**
 *  邮箱验证
 */
function checkEmail($email){
    $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
    if ( preg_match( $pattern, $email ) )
    { return true;}
    else {
        return false;
    }
}

/**
 *  上传图片
 * @param $file  文件
 * @param $dir  地址
 * @param string $mini  是否上传缩略图
 * @return string
 */
function upPic($file,$dir,$mini = ''){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize = 5*1024*1024 ;// 设置附件上传大小 5m
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath = $dir; // 设置附件上传根目录
    $upload->replace    = true;
//    $upload->saveName       =   'msectime'; //自定义函数，采用13位毫秒和3位随机数
    $info = $upload->uploadOne($file);// 上传文件
    if(!$info) {// 上传错误提示错误信息
        $data['tag']    =   1;
        $data['msg']    =   $upload->getError();
        return $data;
    }else{// 上传本地成功
        if($mini){
            $image = new \Think\Image();
            $thumb_file = $dir . $info['savepath'] . $info['savename'];
            $save_path = $dir .$info['savepath'] . 'mini_' . $info['savename'];
            $image->open( $thumb_file )->save( $thumb_file );
            $image->open( $thumb_file )->thumb( 120, 140 )->save( $save_path );
            //                ->text('德兴房产','./data/1.otf',24,'#A7AAA4',\Think\Image::IMAGE_WATER_SOUTHWEST)
            //                $pic = "/Uploads/users/".$info['savepath'].$info['savename'];
            $data['mini_pic']  =   trim($save_path,'.');
            $data['pic']  =   trim($thumb_file,'.');
            $this_pic = ".".$dir.$info['savepath'].$info['savename'];
            unlink($this_pic);
        }else{
            $data = trim($dir,'.').$info['savepath'].$info['savename'];
        }
        return $data;
    }

}

/**
 * 将手机号中间展示*
 */

function changeMobile($phone){
//    $xing = substr($phone,3,4);  //获取手机号中间四位
//    $phone = str_replace($xing,'****',$phone);  //用****进行替换
    //或者
    $phone = substr_replace($phone, '****', 3, 4);
    return $phone;
}

/**
 * 获取表中的数组
 * $sql : category表中的 id
 * page : 页码*个数
 * num  : 个数
 * field : 获取字段
 * picField : 图片id
 *
 */
function getList($table,$sql,$page,$num,$order,$field=""){
    if($field) $list = M($table)->where($sql)->limit($page,$num)->order($order)->field($field)->select();
    else $list = M($table)->where($sql)->limit($page,$num)->order($order)->select();
    return $list;
}

/**
 *  获取详细信息
 */
function getInfo($table,$id){
    return M($table)->where(array('id'=>$id))->find();
}

/**
 * $type 1:公开课 2: 商学网 3:培训资讯 4:课程信息
 * $choose 0: 不推荐 1:推荐
 * $num  个数
 * $sort  排序字段
 */
function getCategory($type,$choose,$num,$sort){
    $sql['type'] = $type;
    if($choose !== null) $sql['if_index'] = $choose;

    return M('category')->where($sql)->limit($num)->order($sort)->select();
}

/**
 *  获取课程信息筛选 sql语句C
 */
function getCourseSql($cate_id){
    $class_type_id =   I('get.class_type_id');
    $class_city_id =   I('get.class_city_id');
    $class_money_id =   I('get.class_money_id');
    $class_take_in_id =   I('get.class_take_in_id');
    $class_time =   I('get.class_time');

    if(!empty($class_type_id) || $class_type_id == "0") {
        cookie("c_class_type_id",$class_type_id);
    }else $class_type_id = cookie("c_class_type_id");

    if(!empty($class_city_id ) || $class_city_id == "0") cookie("c_class_city_id",$class_city_id);
    else $class_city_id = cookie("c_class_city_id");

    if(!empty($class_money_id ) || $class_money_id == "0") cookie("c_class_money_id",$class_money_id);
    else $class_money_id = cookie("c_class_money_id");

    if(!empty($class_take_in_id ) || $class_take_in_id == "0") cookie("c_class_take_in_id",$class_take_in_id);
    else $class_take_in_id = cookie("c_class_take_in_id");

    if(!empty($class_time ) || $class_time == "0") cookie("c_class_time",$class_time);
    else $class_time = cookie("c_class_time");

    $sql = "cate_id = '$cate_id'";
    if($class_type_id) $sql =   $sql." and class_type_id = '$class_type_id'";
    if($class_city_id) $sql =   $sql." and class_city_id = '$class_city_id'";
    if($class_money_id) $sql =   $sql." and class_money_id = '$class_money_id'";

    if($class_take_in_id) $sql =   $sql." and class_take_in_id = '$class_take_in_id'";
    $time = time();
    if($class_time == '1') $sql =   $sql." and UNIX_TIMESTAMP(start_time) >= '$time'"; //招生中
    elseif($class_time == "2") $sql =   $sql." and UNIX_TIMESTAMP(start_time) < '$time'"; //已开讲

    return trim($sql,"and");

}

/**
 *  获取课程信息筛选 sql语句 P
 */
function getOpenSql($cate_id){
    $class_type_id =   I('get.class_type_id');
    $class_city_id =   I('get.class_city_id');
    $class_money_id =   I('get.class_money_id');
    $class_judge =   I('get.class_judge');
    $class_time =   I('get.class_time');

    if(!empty($class_type_id) || $class_type_id == "0") {
        cookie("p_class_type_id",$class_type_id);
    }else $class_type_id = cookie("p_class_type_id");

    if(!empty($class_city_id ) || $class_city_id == "0") cookie("p_class_city_id",$class_city_id);
    else $class_city_id = cookie("p_class_city_id");

    if(!empty($class_money_id ) || $class_money_id == "0") cookie("p_class_money_id",$class_money_id);
    else $class_money_id = cookie("p_class_money_id");

    if(!empty($class_judge ) || $class_judge == "0") cookie("p_class_judge",$class_judge);
    else $class_judge = cookie("p_class_judge");

    if(!empty($class_time ) || $class_time == "0") cookie("p_class_time",$class_time);
    else $class_time = cookie("p_class_time");

    $sql = "cate_id = '$cate_id'";
    if($class_type_id) $sql =   $sql." and class_type_id = '$class_type_id'";
    if($class_city_id) $sql =   $sql." and class_city_id = '$class_city_id'";
    if($class_money_id) $sql =   $sql." and class_money_id = '$class_money_id'";

    if($class_judge) $sql =   $sql." and class_judge = '$class_judge'";
    $time = time();
    if($class_time == '1') $sql =   $sql." and UNIX_TIMESTAMP(start_time) >= '$time'"; //招生中
    elseif($class_time == "2") $sql =   $sql." and UNIX_TIMESTAMP(start_time) < '$time'"; //已开讲

    return trim($sql,"and");

}

/**
 * 清空cookie信息
 */
function delCookie(){
    delCookieP();delCookieC();
}

function delCookieP(){
    cookie("p_class_type_id",null);cookie("p_class_city_id",null);cookie("p_class_money_id",null);
    cookie("p_class_judge",null);cookie("p_class_time",null);
}

function delCookieC(){
    cookie("c_class_type_id",null);cookie("c_class_city_id",null);cookie("c_class_money_id",null);
    cookie("c_class_take_in_id",null);cookie("c_class_time",null);
}

/**
 *  添加到数据库
 */
function addTable($table,$data){
    return M($table)->add($data);
}

/**
 * 获取当前时间
 */
function getTime(){
    return date('Y-m-d H:i:s');
}


//身份证格式验证
function validation_filter_id_card($id_card){
    if(strlen($id_card)==18){
        return idcard_checksum18($id_card);
    }elseif((strlen($id_card)==15)){
        $id_card=idcard_15to18($id_card);
        return idcard_checksum18($id_card);
    }else{
        return false;
    }
}
// 计算身份证校验码，根据国家标准GB 11643-1999
function idcard_verify_number($idcard_base){
    if(strlen($idcard_base)!=17){
        return false;
    }
    //加权因子
    $factor=array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
    //校验码对应值
    $verify_number_list=array('1','0','X','9','8','7','6','5','4','3','2');
    $checksum=0;
    for($i=0;$i<strlen($idcard_base);$i++){
        $checksum += substr($idcard_base,$i,1) * $factor[$i];
    }
    $mod=$checksum % 11;
    $verify_number=$verify_number_list[$mod];
    return $verify_number;
}
// 将15位身份证升级到18位
function idcard_15to18($idcard){
    if(strlen($idcard)!=15){
        return false;
    }else{
        // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
        if(array_search(substr($idcard,12,3),array('996','997','998','999')) !== false){
            $idcard=substr($idcard,0,6).'18'.substr($idcard,6,9);
        }else{
            $idcard=substr($idcard,0,6).'19'.substr($idcard,6,9);
        }
    }
    $idcard=$idcard.idcard_verify_number($idcard);
    return $idcard;
}
// 18位身份证校验码有效性检查
function idcard_checksum18($idcard){
    if(strlen($idcard)!=18){
        return false;
    }
    $idcard_base=substr($idcard,0,17);
    if(idcard_verify_number($idcard_base)!=strtoupper(substr($idcard,17,1))){
        return false;
    }else{
        return true;
    }
}



//课程状态
function  task_course_status($id){
    $id = intval($id);
    switch($id){
        case 1: return '未开始'; break;
        case 2: return '选课中'; break;
        case 3: return '已结束'; break;
    }
    return ;
}
//课程时间(天)
function  task_course_day($id){
    $id = intval($id);
    switch($id){
        case 1: return '星期一'; break;
        case 2: return '星期二'; break;
        case 3: return '星期三'; break;
        case 4: return '星期四'; break;
        case 5: return '星期五'; break;
    }
    return ;
}
//课程时间(课节)
function  task_course_time($id){
    $id = intval($id);
    switch($id){
        case 1: return '一,二节'; break;
        case 2: return '三,四节'; break;
        case 3: return '五,六节'; break;
        case 4: return '七,八节'; break;
    }
    return ;
}